package com.paranoid.flashcard.model;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.database.Cursor;
import android.graphics.Bitmap;

import com.paranoid.flashcard.dao.FlashcardDao;

public class Deck {

	public static final String KEY_ID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_STATUS = "status";
    public static final String KEY_FEATURED = "featured";

    public final static String DECK_TABLE_CREATE = "CREATE TABLE "
        + Deck.TABLE_NAME + 
        " (" + Deck.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        Deck.KEY_TITLE + " TEXT, " + 
        Deck.KEY_STATUS + " INTEGER, " +
        Deck.KEY_FEATURED + " INTEGER);";
    
    private static final int POS_ID = 0;
    private static final int POS_TITLE = 1;
    private static final int POS_STATUS = 2;
    private static final int POS_FEATURED = 3;
    
    public enum Featured {YES, NO};
    public enum DownloadStatus {UPDATING, REMOTE, DOWNLOADING, LOCAL, DELETING};
    
	public static final String TABLE_NAME = "deck";

	private final List<Card> flashCards;
	private String name;
	private String title;
	

	public Deck(String name, String title, Card ...flashCards) {
		this.flashCards = new ArrayList<Card>();
		Collections.addAll(this.flashCards, flashCards);
		this.name = name;
		this.title = title;
	}
	
	public Deck(String name, long deckId, Cursor cardCursor) {
		this.name = name;
		flashCards = new ArrayList<Card>();
		while (cardCursor.moveToNext()) {
			long id = cardCursor.getLong(0);
			String sideA = cardCursor.getString(1);
			String sideB = cardCursor.getString(2);
            int masteryLevel = cardCursor.getInt(3);
			FlashcardDao dao = FlashcardDao.getInstance(null);
			Bitmap imageA = dao.getImage(deckId, id);
			flashCards.add(new Card(id, sideA, sideB, imageA, masteryLevel));
		}
	}
	
	public List<Card> getFlashCards() {
		return this.flashCards;
	}
	
	public String getName() {
		return this.name;
	}
	
	public String getTitle() {
		return this.title;
	}
	
	public void resetMasteryLevels() {
		for (Card card : flashCards) {
			card.resetMasteryLevel();
		}
	}
	
	public int getMasteryLevel() {
		int min = Integer.MAX_VALUE;
		int tmp;
		for (Card card : flashCards) {
			tmp = card.getMasteryLevel();
			if (tmp < min)
				min = tmp;
		}
		return min;
	}
	
	public void addFlashCard(Card card) {
		flashCards.add(card);
	}
	
	public Card getFlashCardByPosition(int position) {
		return flashCards.get(position);
	}
	
	public int size() {
		return flashCards.size();
	}

	public static DownloadStatus getDownloadStatus(Cursor c) {
	    int status = c.getInt(POS_STATUS);
	    return DownloadStatus.values()[status];
	}

    public static long getId(Cursor c) {
        return c.getLong(POS_ID);
    }

    public static String getTitle(Cursor c) {
        return c.getString(POS_TITLE);
    }

    public static Featured getFeatured(Cursor c) {
        int featured = c.getInt(POS_FEATURED);
        return Featured.values()[featured];
    }

    public String toString() {
        return title;
    }

}
